package cn.b.sky.mes.hr.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import cn.b.sky.mes.hr.constans.HrConstans;
import cn.b.sky.mes.hr.model.Performance;
import cn.b.sky.mes.hr.model.ShiftingPermission;
import cn.b.sky.mes.hr.model.condition.PerformanceCondition;
import cn.b.sky.mes.hr.service.PerformanceService;
import cn.b.sky.mes.hr.service.ShiftingPermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.b.sky.common.file.Excel;
import cn.b.sky.common.file.ExcelModel;
import cn.b.sky.common.file.GridModel;
import cn.b.sky.user.SecurityContextUtil;
import cn.b.sky.user.controller.SecurityController;

@Controller
@RequestMapping(value = "/hr_productionPerformance/*")
public class ProductionPerformanceStatisticsController extends SecurityController {

  @Autowired
  private PerformanceService performanceService;

  @Resource
  private ShiftingPermissionService shiftingPermissionService;

  /**
   *  
   * <strong>Title : performanceStatistics<br></strong>
   * <strong>Description : </strong>查询绩效数据列表<br> 
   * <strong>Create on : 2016-3-31 下午3:17:43<br></strong>
   * <p>
   * @param navTabId
   * @param condition
   * @param model
   * @return
   * String
   * @throws 
   * @author ZiQiang.Zhao<br>
   * @version <strong>v1.0.0</strong><br>
   * <br>
   * <strong>修改历史:</strong><br>
   * 修改人 | 修改日期 | 修改描述<br>
   * -------------------------------------------<br>
   * <br>
   * <br>
   */
  @RequestMapping("performanceStatistics")
  public String performanceStatistics(String navTabId, PerformanceCondition condition, Model model, HttpSession session) {
    model.addAttribute(NAVTABID, navTabId);
    Date today = new Date();
    condition.setSiteCode(this.getSiteCode());
    //获取当前用户的工作中心权限
    List<ShiftingPermission> workCenters = shiftingPermissionService.getPermissionsByUser(this.getSiteCode(),
        SecurityContextUtil.getCurrentUserName(), null);
    //没有工作中心权限则不过滤
    if (workCenters == null || workCenters.size() <= 0) {
      condition.setData(new ArrayList<Performance>());
      condition.setTotalCount(0);
    } else {
      condition.setPermission(workCenters);
      condition.setData(performanceService.getPerformanceListByCondition(condition));
      condition.setTotalCount(performanceService.getPerformanceCountByCondition(condition));
    }
    session.setAttribute(HrConstans.PRODUCTION_PREF_CONDITION, condition);

    model.addAttribute("today", today);
    model.addAttribute("page", condition);
    return "hr/performance/productionPerf_statistics";
  }

  /**
   * 
   * <strong>Title : exportDateReport<br></strong>
   * <strong>Description : </strong>导出Excel<br> 
   * <strong>Create on : 2016-3-31 下午3:17:27<br></strong>
   * <p>
   * @param session
   * @param condition
   * @param response
   * @param tableMap
   * @param arrangement
   * @throws IOException
   * void
   * @throws 
   * @author ZiQiang.Zhao<br>
   * @version <strong>v1.0.0</strong><br>
   * <br>
   * <strong>修改历史:</strong><br>
   * 修改人 | 修改日期 | 修改描述<br>
   * -------------------------------------------<br>
   * <br>
   * <br>
   */
  @RequestMapping(value = "exportDateReport")
  @ResponseBody
  public void exportDateReport(HttpSession session, PerformanceCondition condition, HttpServletResponse response,
      String tableMap, String arrangement) throws IOException {

    PerformanceCondition condition0 = (PerformanceCondition) session
        .getAttribute(HrConstans.PRODUCTION_PREF_CONDITION);
    if (condition0 != null) {
      condition = condition0;
    }

    condition.setSiteCode(this.getSiteCode());
    condition.setUserCode(SecurityContextUtil.getCurrentUserName());
    List<Map<String, Object>> list = performanceService.selectAllPerformanceReportMapList(condition);

    Excel excel = new Excel();
    excel.setTableMap(tableMap);
    ExcelModel model = new GridModel(excel.getTable("iTable"), list);
    excel.addExcelModel(model);

    excel.doExport(response, "生产绩效统计");
  }

}
